﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EngineOfEvermore.Editor.Common;
using System.Diagnostics;
using WeifenLuo.WinFormsUI.Docking;
using System.Windows.Forms;
using EngineOfEvermore.Editor.Common.Controls;

namespace EngineOfEvermore.Editor.AnimationEditor.View
{
    class View : IView
    {
        IInputListener _inputListener;
        AnimationExplorer _animationExplorer;
        MainForm _mainForm;
        GorgonRenderTargetManager _renderTargetManager;
        ImageMapView _imageMapView;
        OpenResourceDialog _openResourceDialog; 
        LayoutManager _layoutManager;

        public IInputListener InputListener
        {
            get { return _inputListener; }
        }

        public IImageMapView ImageMapView
        {
            get { return _imageMapView; }
        }

        public View( IInputListener inputListener,
            GorgonRenderTargetManager renderTargetManager,
            IViewConfig config )
        {
            Debug.Assert( inputListener != null );
            Debug.Assert( renderTargetManager != null );

            _inputListener = inputListener;
            _renderTargetManager = renderTargetManager;

            _mainForm = new MainForm( inputListener );
            _mainForm.Show();
            _mainForm.FormClosed += _mainFormClosed;
            _mainForm.DockPanel.ActiveDocumentChanged += _activeDocumentChanged;

            _animationExplorer = new AnimationExplorer( inputListener );
            _imageMapView = new ImageMapView( inputListener, renderTargetManager );
            _imageMapView.ContentEnabled = false;

            _layoutManager = new LayoutManager( config, 
                _mainForm, 
                _animationExplorer, 
                _imageMapView );

            _layoutManager.ApplyLayout();

            _openResourceDialog = new OpenResourceDialog( "ImageMaps", ".ximp" );
        }

        public IDocumentView CreateNewDocument()
        {
            DocumentView newDoc = new DocumentView( _renderTargetManager, _inputListener );
            newDoc.Show( _mainForm.DockPanel );

            newDoc.FormClosing += new FormClosingEventHandler( _docViewFormClosing );
            
            return newDoc;
        }

        public void ShowAnimationExplorer()
        {
            _animationExplorer.Show( _mainForm.DockPanel );
        }

        public void ShowImageMapView()
        {
            _imageMapView.Show( _mainForm.DockPanel );
        }

        public void BringDocumentToFocus( IDocumentView doc )
        {
            DocumentView docForm = (DocumentView) doc;

            DockWindow dockWindow = _mainForm.DockPanel.DockWindows[DockState.Document];
            DockPane dockPane = dockWindow.NestedPanes.Single( x => x.Contains( docForm ) );
            dockPane.ActiveContent = docForm;

            docForm.Focus();
        }

        public String ShowImageMapLoadingDialog()
        {
            DialogResult result = _openResourceDialog.ShowDialog();

            if ( result == DialogResult.OK )
            {
                return _openResourceDialog.SelectedResourceName;
            }
            else
            {
                return String.Empty;
            }
        }

        void _mainFormClosed( object sender, FormClosedEventArgs e )
        {
            _layoutManager.SaveLayout();
        }

        void _docViewFormClosing( object sender, FormClosingEventArgs e )
        {
            _inputListener.OnDocumentClosing( (IDocumentView)sender, e );
        }

        void _activeDocumentChanged( object sender, EventArgs e )
        {
            IDocumentView activeDocument = (IDocumentView)_mainForm.DockPanel.ActiveDocument;
            _inputListener.OnDocumentActivated( activeDocument );
        }
    }
}
